package com.changgou.web.gateway.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpCookie;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Service;

@Service
public class AuthService {

    // 注入redis模板
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    // 从cookie中获取jti的值
    public String getJtiFromCookie(ServerHttpRequest request) {
        HttpCookie uid = request.getCookies().getFirst("uid");
        if (uid != null){
            String jti = uid.getValue();
            return  jti;
        }
        return null;
    }

    // 从redis中根据jti的key获取jwt的值（获取令牌）
    public String getJwtFromRedis(String jti) {
        String jwt = stringRedisTemplate.boundValueOps(jti).get();
        return jwt;
    }
}
